//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.doc.server.commons;

import com.farm.core.time.TimeTool;
import com.farm.parameter.FarmParameterService;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class FarmDocFiles {
    private static final Logger log = Logger.getLogger(FarmDocFiles.class);

    public FarmDocFiles() {
    }

    public static String generateDir() {
        String dirPath = File.separator + TimeTool.getTimeDate12().substring(0, 4) + File.separator + TimeTool.getTimeDate12().substring(4, 6) + File.separator + TimeTool.getTimeDate12().substring(6, 8) + File.separator + TimeTool.getTimeDate12().substring(8, 10) + File.separator;
        File accessFile = new File(getFileDirPath() + dirPath);
        accessFile.mkdirs();
        return dirPath;
    }

    public static File renameFile(File file, String filename) {
        String c = file.getParent();
        File mm = new File(c + File.separator + filename);
        if (!file.renameTo(mm)) {
            throw new RuntimeException("file rename fail!");
        } else {
            return mm;
        }
    }

    public static String getFileDirPath() {
        String path = FarmParameterService.getInstance().getParameter("config.doc.dir");

        try {
            if (path.startsWith("WEBROOT")) {
                path = path.replace("WEBROOT", FarmParameterService.getInstance().getParameter("farm.constant.webroot.path"));
            }

            String separator = File.separator;
            if (separator.equals("\\")) {
                separator = "\\\\";
            }

            path = path.replaceAll("\\\\", "/").replaceAll("/", separator);
        } catch (Exception var2) {
            log.warn(path + ":路径地址有误！");
            path = DocumentConfig.getString("config.doc.dir");
        }

        return path;
    }

    public static String getExName(String fileName) {
        return fileName.substring(fileName.lastIndexOf("."));
    }

    public static List<String> getFilesIdFromHtml(String html) {
        List<String> list = new ArrayList();
        if (html == null) {
            return list;
        } else {
            Document doc = Jsoup.parse(html);
            Iterator var4 = doc.getElementsByTag("img").iterator();

            Element node;
            String urlStr;
            String splits;
            while(var4.hasNext()) {
                node = (Element)var4.next();
                urlStr = node.attr("src");
                if (urlStr.indexOf(DocumentConfig.getString("config.doc.download.url")) >= 0) {
                    splits = urlStr.substring(urlStr.indexOf(DocumentConfig.getString("config.doc.download.url")) + DocumentConfig.getString("config.doc.download.url").length());
                    list.add(splits);
                }
            }

            var4 = doc.getElementsByTag("a").iterator();

            while(var4.hasNext()) {
                node = (Element)var4.next();
                urlStr = node.attr("href");
                if (urlStr.indexOf(DocumentConfig.getString("config.doc.download.url")) >= 0) {
                    splits = urlStr.substring(urlStr.indexOf(DocumentConfig.getString("config.doc.download.url")) + DocumentConfig.getString("config.doc.download.url").length());
                    list.add(splits);
                }
            }

            return list;
        }
    }

    public static void copyFile(File file, String newPath) {
        if (file.exists()) {
            InputStream inStream = null;
            FileOutputStream fs = null;

            try {
                inStream = new FileInputStream(file);
                fs = new FileOutputStream(newPath + file.getName());
                byte[] buffer = new byte[1444];

                int byteread;
                while((byteread = inStream.read(buffer)) != -1) {
                    fs.write(buffer, 0, byteread);
                }
            } catch (FileNotFoundException var17) {
                log.error(var17.getMessage());
            } catch (IOException var18) {
                log.error(var18.getMessage());
            } finally {
                try {
                    inStream.close();
                    fs.close();
                } catch (IOException var16) {
                    ;
                }

            }
        }

    }

    public static Long saveFile(InputStream inStream, String filename, String newPath) {
        FileOutputStream fs = null;

        try {
            fs = new FileOutputStream(newPath + filename);
            byte[] buffer = new byte[1444];

            int byteread;
            while((byteread = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteread);
            }
        } catch (FileNotFoundException var16) {
            log.error(var16.getMessage());
        } catch (IOException var17) {
            log.error(var17.getMessage());
        } finally {
            try {
                inStream.close();
                fs.close();
            } catch (IOException var15) {
                ;
            }

        }

        File file = new File(newPath + filename);
        return file.length();
    }
}
